package com.keshi.openplatform.payment.event;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.keshi.openplatform.payment.mq.SendPayMentMQStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.stereotype.Component;

@Component
public class PaymentEventListener {
    @Autowired
    private SendPayMentMQStream sendPayMentMQStream;
    @Autowired
    private ObjectMapper objectMapper;

    @EventListener
    public void onEvent(PaymentEvent event) {
        MessageChannel messageChannel = sendPayMentMQStream.message_channel();
        try {
            messageChannel.send(new GenericMessage<String>(objectMapper.writeValueAsString(event)));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

}
